﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bordspil.Models;

namespace Bordspil.Controllers
{
    public class GameController : Controller
    {
        private IAppRepository m_repo = null;

        public GameController()
        {
            m_repo = new AppRepository();
        }

        public ActionResult GamesInProgress()
        {
            var games = (from x in m_repo.GetAllGames()
                        where x.isDone == false
                        select x);

            return View(games);
        }
        
        public ActionResult PairGame()
        {
            return View();
        }
          
        public ActionResult JoinPairGame(int id)
        {
            var game = (from x in m_repo.GetAllGames()
                        where x.ID == id
                        select x).SingleOrDefault();

            var cards = (from x in m_repo.GetAllCardsForInstance()
                         where x.GameInstID == id
                         join c in m_repo.GetAllCards()
                         on x.DeckOfCardsID equals c.ID
                         select c).ToList();
                       
            return View("PairGame", new GameViewModel(cards, game));
        }

        public ActionResult Ludo()
        {
            return View();
        }

        public ActionResult GamesWithCards()
        {
            return View();
        }

        public ActionResult Boardgames()
        {
            return View();
        }

        [HttpGet]
        public ActionResult CreatePairGame()
        {
            return View();
        }

        [HttpGet]
        public ActionResult CreateLudoGame()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreatePairGame(Bordspil.Models.GameInstance gameI)
        {
            var cards = (from x in m_repo.GetAllCards()
                         select x).ToList();

            Random rng = new Random();

            int n = cards.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                var value = cards[k];
                cards[k] = cards[n];
                cards[n] = value;
            }

            List<Deck> fifteenPairs = new List<Deck>();

            for (var i = 1; i <= 12; i++)
            {
                var aPair = (from x in cards
                             where x.cardID == i
                             select x).Take(2);

                fifteenPairs.AddRange(aPair);
            }

            n = fifteenPairs.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                var value = fifteenPairs[k];
                fifteenPairs[k] = fifteenPairs[n];
                fifteenPairs[n] = value;
            }

            var newGameI = new GameInstance();

            int uID = (from c in m_repo.GetAllUsers()
                       where c.username == User.Identity.Name
                       select c.ID).First();

            newGameI.name = gameI.name;
            newGameI.gameName = "PairGame";
            newGameI.player1ID = (from c in m_repo.GetAllUsers()
                                  where c.username == User.Identity.Name
                                  select c.ID).First();

            newGameI.player2ID = 0;
            newGameI.player3ID = 0;
            newGameI.player4ID = 0;
            newGameI.player1Score = 0;
            newGameI.player2Score = 0;
            newGameI.player3Score = 0;
            newGameI.player4Score = 0;
            newGameI.isDone = false;
            newGameI.isWaiting = true;
            newGameI.winnerID = 0;

            m_repo.AddGameInstance(newGameI);

            m_repo.AddDeckToInstance(fifteenPairs);

            return View( "PairGame", new GameViewModel(fifteenPairs, gameI));
        }
    }
}
